package com.controller;

import com.pojo.SysUser;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
public class ExampleController {
    @Autowired
    UserService userService;
    @RequestMapping("/hello/{id}")
    public String getUser(@PathVariable("id")Long usrId, Model model){
        SysUser user=userService.getById(usrId);
        model.addAttribute("user",user);
        return "demo/hello";
    }

    @RequestMapping("/string")
    public String getString(Model model, HttpServletRequest request){
        model.addAttribute("userName","ktjiaoyu");
        request.setAttribute("test", "request");
        request.getSession().setAttribute("test", "session");
        model.addAttribute("date",new Date());
        return "demo/string";
    }

    @RequestMapping("/inline")
    public String inline(Model model){
        model.addAttribute("userName","ktjiaoyu");
        return "demo/inline";
    }

    @RequestMapping("/if")
    public String ifunless(Model model){
        model.addAttribute("flag","yes");
        return "demo/if";
    }

    @RequestMapping("/list")
    public String list(Model model){
        List<SysUser> list=userService.list();
        model.addAttribute("users",list);
        return "demo/list";
    }

    @RequestMapping("/index")
    public String index(){
        return "demo/index";
    }
}
